# -*- coding: utf-8 -*-
# @Time    : 2024/12/9  15:49
# @Author  : Nabby
# @FileName: jasco_declaration_information_impl.py
# @Desc    : 申报信息
from app import db
from infrastructure.po.mode.jasco_loading_order import (
    JascoLoadingOrder,
)
from sqlalchemy import and_, or_, desc, asc


class JascoLoadingOrderImpl(object):

    @classmethod
    def get_loading_order_list(cls, declaration_information_id, page=1, page_size=999):
        query = db.session.query(JascoLoadingOrder).filter(
            JascoLoadingOrder.declaration_information_id == declaration_information_id
        )
        return (
            query.order_by(JascoLoadingOrder.create_time.desc()).paginate(
                page=page, per_page=page_size
            ),
            query.count(),
        )

    @classmethod
    def add_loading_order(
        cls,
        declaration_information_id,
        container_number,
        english_name,
        chinese_name,
        customs_code,
        products_num,
        chinese_material,
        english_material,
        chinese_use,
        english_use,
        brand,
        model,
        unit,
        price,
        currency,
        total_price,
        net_weight,
        source_area,
        final_destination,
    ):

        jasco_loading_order = JascoLoadingOrder(
            container_number=container_number,
            english_name=english_name,
            chinese_name=chinese_name,
            customs_code=customs_code,
            products_num=products_num,
            chinese_material=chinese_material,
            english_material=english_material,
            chinese_use=chinese_use,
            english_use=english_use,
            brand=brand,
            model=model,
            unit=unit,
            price=price,
            currency=currency,
            total_price=total_price,
            net_weight=net_weight,
            source_area=source_area,
            final_destination=final_destination,
            declaration_information_id=declaration_information_id,
        )
        db.session.add(jasco_loading_order)
        db.session.flush()
        db.session.commit()
        return True

    @classmethod
    def edit_loading_order(
        cls,
        id,
        declaration_information_id,
        container_number,
        english_name,
        chinese_name,
        customs_code,
        products_num,
        chinese_material,
        english_material,
        chinese_use,
        english_use,
        brand,
        model,
        unit,
        price,
        currency,
        total_price,
        net_weight,
        source_area,
        final_destination,
    ):
        jasco_loading_order = JascoLoadingOrder.query.filter(
            JascoLoadingOrder.id == id, JascoLoadingOrder.declaration_information_id == declaration_information_id
        ).first()
        if jasco_loading_order is None:
            return False
        jasco_loading_order.container_number = container_number
        jasco_loading_order.english_name = english_name
        jasco_loading_order.chinese_name = chinese_name
        jasco_loading_order.customs_code = customs_code
        jasco_loading_order.products_num = products_num
        jasco_loading_order.chinese_material = chinese_material
        jasco_loading_order.english_material = english_material
        jasco_loading_order.chinese_use = chinese_use
        jasco_loading_order.english_use = english_use
        jasco_loading_order.brand = brand
        jasco_loading_order.model = model
        jasco_loading_order.unit = unit
        jasco_loading_order.price = price
        jasco_loading_order.currency = currency
        jasco_loading_order.total_price = total_price
        jasco_loading_order.net_weight = net_weight
        jasco_loading_order.source_area = source_area
        jasco_loading_order.final_destination = final_destination
        db.session.commit()
        return True

    @classmethod
    def delete_loading_order(cls, id, declaration_information_id):
        jasco_loading_order = JascoLoadingOrder.query.filter(
            JascoLoadingOrder.id == id, JascoLoadingOrder.declaration_information_id == declaration_information_id
        ).first()
        if jasco_loading_order is None:
            return False
        db.session.delete(jasco_loading_order)
        db.session.commit()
        return True

